﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBTS.DAL.EF;
using IBTS.BO.Objects;
using System.Reflection;

namespace IBTS.DAL.Core
{
    public static partial class DALUtils
    {
        public static class KeysDB
        {
            public static List<KeysItem> GetAllKeys()
            {
                List<KeysItem> toReturn = new List<KeysItem>();

                try
                {
                    using (IBTSEntities ctx = new IBTSEntities())
                    {
                        toReturn = Converter.DBToView((from r in ctx.Keys
                                                       select r).ToList());
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return toReturn;
            }

            public static int Insert(KeysItem toInsert)
            {
                int toReturn = 0;

                try
                {
                    Keys dbObject = Converter.ViewToDB(toInsert);

                    using (IBTSEntities ctx = new IBTSEntities())
                    {
                        ctx.AddToKeys(dbObject);

                        toReturn = ctx.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return toReturn;
            }

            public static int Update(KeysItem toUpdate)
            {
                int toReturn = 0;

                try
                {
                    using (IBTSEntities ctx = new IBTSEntities())
                    {
                        Keys dbObject = (from r in ctx.Keys where r.Id == toUpdate.Id select r).FirstOrDefault();

                        if (dbObject != null)
                        {
                            dbObject.Name = toUpdate.Name;
                            dbObject.Value = toUpdate.Value;
                        }

                        toReturn = ctx.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return toReturn;
            }

            public static int Delete(long idToDelete)
            {
                int toReturn = 0;

                try
                {
                    using (IBTSEntities ctx = new IBTSEntities())
                    {
                        Keys dbObject = (from r in ctx.Keys where r.Id == idToDelete select r).FirstOrDefault();

                        ctx.DeleteObject(dbObject);

                        toReturn = ctx.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return toReturn;
            }

            public static class Converter
            {
                public static Keys ViewToDB(KeysItem toConvert)
                {
                    Keys toReturn = new Keys();

                    try
                    {
                        CopyObject(toConvert, toReturn);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    return toReturn;
                }

                public static KeysItem DBToView(Keys toConvert)
                {
                    KeysItem toReturn = new KeysItem();

                    try
                    {
                        CopyObject(toConvert, toReturn);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    return toReturn;
                }

                public static List<Keys> ViewToDB(List<KeysItem> toConvert)
                {
                    List<Keys> toReturn = new List<Keys>();

                    try
                    {
                        toReturn = (from r in toConvert select ViewToDB(r)).ToList();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    return toReturn;
                }

                public static List<KeysItem> DBToView(List<Keys> toConvert)
                {
                    List<KeysItem> toReturn = new List<KeysItem>();

                    try
                    {
                        toReturn = (from r in toConvert select DBToView(r)).ToList();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    return toReturn;
                }

                private static void CopyObject<T1, T2>(T1 source, T2 destination)
                {
                    try
                    {
                        Type type = source.GetType();

                        PropertyInfo[] myObjectFields = type.GetProperties();

                        foreach (PropertyInfo sourceProp in myObjectFields)
                        {
                            PropertyInfo destProp = destination.GetType().GetProperty(sourceProp.Name);

                            if (destProp != null && destProp.PropertyType == sourceProp.PropertyType)
                            {
                                object value = sourceProp.GetValue(source, null);

                                destProp.SetValue(destination, value, null);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }
    }
}
